home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_noises.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-17  |  9.6 KB  |  337 lines

  1. /************************************************************************
  2.  * noises.h - various noise-based patterns
  3.  *
  4.  * Author: Larry Gritz (gritzl@acm.org), though they're obvious to any
  5.  *         experience shader writer.
  6.  *
  7.  * Reference:
  8.  *   _Advanced RenderMan: Creating CGI for Motion Picture_, 
  9.  *   by Anthony A. Apodaca and Larry Gritz, Morgan Kaufmann, 1999.
  10.  *
  11.  * $Revision: 1.7 $    $Date: 2006/03/17 17:31:39 $
  12.  *
  13.  ************************************************************************/
  14.  
  15.  
  16. #ifndef NOISES_H
  17. #define NOISES_H 1
  18.  
  19.  
  20. #ifndef FILTERWIDTH_H
  21. #include "k3d_filterwidth.h"    /* Needed for filterwidth and friends */
  22. #endif
  23.  
  24. #ifndef PATTERNS_H
  25. #include "k3d_patterns.h"    /* Needed for filteredabs */
  26. #endif
  27.  
  28.  
  29.  
  30.  
  31. #ifndef snoise
  32. /*
  33.  * Signed noise -- the original Perlin kind with range (-1,1) We prefer
  34.  * signed noise to regular noise mostly because its average is zero.
  35.  * We define three simple macros:
  36.  *   snoise(p) - Perlin noise on either a 1-D (float) or 3-D (point) domain.
  37.  *   snoisexy(x,y) - Perlin noise on a 2-D domain.
  38.  *   vsnoise(p) - vector-valued Perlin noise on either 1-D or 3-D domain.
  39.  */
  40. #define snoise(p) (2 * (float noise(p)) - 1)
  41. #endif
  42.  
  43. #define snoise2(x) (2.5*(noise(x)-0.5))   /* need to check this one */
  44. #define snoisexy(x,y) (2 * (float noise(x,y)) - 1)
  45. #define vsnoise(p) (2 * (vector noise(p)) - 1)
  46. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  47. #define fnoise(p,width) (noise(p) * (1-smoothstep (0.2,0.75,width)))
  48. #define adjustNoise(x, y, minVal, maxVal) snoisexy (x,y) * ((maxVal)-(minVal)+(minVal))
  49.  
  50.  
  51.  
  52. /* uniformly distributed noise
  53.  *
  54.  */
  55. #define udn(x,lo,hi) (smoothstep(.25, .75, noise(x)) * ((hi) - (lo)) + (lo))
  56. #define udn2(x,y,lo,hi) (smoothstep(.25, .75, noise(x,y)) * ((hi)-(lo))+(lo))
  57.  
  58.  
  59. /* If we know the filter size, we can crudely antialias snoise by fading
  60.  * to its average value at approximately the Nyquist limit.
  61.  */
  62. #define filteredsnoise(p,width) (snoise(p) * (1-smoothstep (0.2,0.75,width)))
  63. #define filteredvsnoise(p,width) (vsnoise(p) * (1-smoothstep (0.2,0.75,width)))
  64.  
  65.  
  66.  
  67. /* fractional Brownian motion
  68.  * Inputs: 
  69.  *    p, filtwidth   position and approximate inter-pixel spacing
  70.  *    octaves        max # of octaves to calculate
  71.  *    lacunarity     frequency spacing between successive octaves
  72.  *    gain           scaling factor between successive octaves
  73.  */
  74. float fBm (point p; float filtwidth;
  75.            uniform float octaves, lacunarity, gain)
  76. {
  77.     uniform float amp = 1;
  78.     varying point pp = p;
  79.     varying float sum = 0, fw = filtwidth;
  80.     uniform float i;
  81.  
  82.     for (i = 0;  i < octaves;  i += 1) {
  83. #pragma nolint
  84.     sum += amp * filteredsnoise (pp, fw);
  85.     amp *= gain;  pp *= lacunarity;  fw *= lacunarity;
  86.     }
  87.     return sum;
  88. }
  89.  
  90.  
  91. /* Typical use of fBm: */
  92. #define fBm_default(p)  fBm (p, filterwidthp(p), 4, 2, 0.5)
  93.  
  94.  
  95.  
  96.  
  97.  
  98. /* A vector-valued antialiased fBm. */
  99. vector
  100. vfBm (point p; float filtwidth;
  101.       uniform float octaves, lacunarity, gain)
  102. {
  103.     uniform float amp = 1;
  104.     varying point pp = p;
  105.     varying vector sum = 0;
  106.     varying float fw = filtwidth;
  107.     uniform float i;
  108.  
  109.     for (i = 0;  i < octaves;  i += 1) {
  110. #pragma nolint
  111.     sum += amp * filteredvsnoise (pp, fw);
  112.     amp *= gain;  pp *= lacunarity;  fw *= lacunarity;
  113.     }
  114.     return sum;
  115. }
  116.  
  117.  
  118. /* Typical use of vfBm: */
  119. #define vfBm_default(p)  vfBm (p, filterwidthp(p), 4, 2, 0.5)
  120.  
  121.  
  122.  
  123. /* The stuff that Ken Musgrave calls "VLNoise" */
  124. #define VLNoise(Pt,scale) (snoise(vsnoise(Pt)*scale+Pt))
  125. #define filteredVLNoise(Pt,fwidth,scale) \
  126.             (filteredsnoise(filteredvsnoise(Pt,fwidth)*scale+Pt,fwidth))
  127.  
  128.  
  129. float
  130. VLfBm (point p; float filtwidth;
  131.        uniform float octaves, lacunarity, gain, scale)
  132. {
  133.     uniform float amp = 1;
  134.     varying point pp = p;
  135.     varying float sum = 0;
  136.     varying float fw = filtwidth;
  137.     uniform float i;
  138.  
  139.     for (i = 0;  i < octaves;  i += 1) {
  140. #pragma nolint
  141.     sum += amp * filteredVLNoise (pp, fw, scale);
  142.     amp *= gain;  pp *= lacunarity;  fw *= lacunarity;
  143.     }
  144.     return sum;
  145. }
  146.  
  147.  
  148. /* Typical use of vfBm: */
  149. #define VLfBm_default(p)      VLfBm (p, filterwidthp(p), 4, 2, 0.5, 1.0)
  150.  
  151.  
  152.  
  153.  
  154. /* Antialiased turbulence.  Watch out -- the abs() call introduces infinite
  155.  * frequency content, which makes our antialiasing efforts much trickier!
  156.  */
  157. float turbulence (point p; float filtwidth;
  158.                   uniform float octaves, lacunarity, gain)
  159. {
  160.     extern float du, dv;   /* Needed for filterwidth macro */
  161.     uniform float amp = 1;
  162.     varying point pp = p;
  163.     varying float sum = 0, fw = filtwidth;
  164.     uniform float i;
  165.  
  166.     for (i = 0;  i < octaves;  i += 1) {
  167. #pragma nolint
  168.     float n = filteredsnoise (pp, fw);
  169.     sum += amp * filteredabs (n, fw);
  170.     amp *= gain;  pp *= lacunarity;  fw *= lacunarity;
  171.     }
  172.     return sum;
  173. }
  174.  
  175.  
  176. /* Typical use of turbulence: */
  177. #define turbulence_default(p)  turbulence (p, filterwidthp(p), 4, 2, 0.5)
  178.  
  179.  
  180.  
  181.  
  182. /***************************************************************************
  183.  * Voronoi cell noise (a.k.a. Worley noise) functions
  184.  *
  185.  * These functions assume that space is filled with "features" (points
  186.  * of interest).  There are interestingpatterns we can make by
  187.  * figuring out which feature we are closest to, or to what extent
  188.  * we're on the boundary between two features.  Several varieties of
  189.  * these computations are below, categorized by the dimension of their
  190.  * domains, and the number of close features they are interested in.
  191.  *
  192.  * All these functions have similar inputs:
  193.  *   P      - position to test (for 3-D varieties; 2-D varieties use ss,tt)
  194.  *   jitter - how much to jitter the cell center positions (1 is typical,
  195.  *             smaller values make a more regular pattern, larger values
  196.  *             make a more jagged pattern; use jitter >1 at your risk!).
  197.  * And outputs:
  198.  *   f_n    - distance to the nth nearest feature (f1 is closest, f2 is
  199.  *            the distance to the 2nd closest, etc.)
  200.  *   pos_n  - the position of the nth nearest feature.  For 2-D varieties,
  201.  *            these are instead spos_n and tpos_n.
  202.  ***************************************************************************/
  203.  
  204. /* Voronoi cell noise (a.k.a. Worley noise) -- 3-D, 1-feature version. */
  205. void
  206. voronoi_f1_3d (point P;
  207.            float jitter;
  208.            output float f1;
  209.            output point pos1;
  210.     )
  211. {
  212.     point thiscell = point (floor(xcomp(P))+0.5, floor(ycomp(P))+0.5,
  213.                 floor(zcomp(P))+0.5);
  214.     f1 = 1000;
  215.     uniform float i, j, k;
  216.     for (i = -1;  i <= 1;  i += 1) {
  217.         for (j = -1;  j <= 1;  j += 1) {
  218.             for (k = -1;  k <= 1;  k += 1) {
  219.         point testcell = thiscell + vector(i,j,k);
  220.                 point pos = testcell + 
  221.                     jitter * (vector cellnoise (testcell) - 0.5);
  222.         vector offset = pos - P;
  223.                 float dist = offset . offset; /* actually dist^2 */
  224.                 if (dist < f1) {
  225.                     f1 = dist;  pos1 = pos;
  226.                 }
  227.             }
  228.     }
  229.     }
  230.     f1 = sqrt(f1);
  231. }
  232.  
  233.  
  234. /* Voronoi cell noise (a.k.a. Worley noise) -- 3-D, 2-feature version. */
  235. void
  236. voronoi_f1f2_3d (point P;
  237.          float jitter;
  238.          output float f1;  output point pos1;
  239.          output float f2;  output point pos2;
  240.     )
  241. {
  242.     point thiscell = point (floor(xcomp(P))+0.5, floor(ycomp(P))+0.5,
  243.                 floor(zcomp(P))+0.5);
  244.     f1 = f2 = 1000;
  245.     uniform float i, j, k;
  246.     for (i = -1;  i <= 1;  i += 1) {
  247.         for (j = -1;  j <= 1;  j += 1) {
  248.             for (k = -1;  k <= 1;  k += 1) {
  249.         point testcell = thiscell + vector(i,j,k);
  250.                 point pos = testcell + 
  251.                     jitter * (vector cellnoise (testcell) - 0.5);
  252.         vector offset = pos - P;
  253.                 float dist = offset . offset; /* actually dist^2 */
  254.                 if (dist < f1) {
  255.                     f2 = f1;  pos2 = pos1;
  256.                     f1 = dist;  pos1 = pos;
  257.                 } else if (dist < f2) {
  258.                     f2 = dist;  pos2 = pos;
  259.         }
  260.             }
  261.     }
  262.     }
  263.     f1 = sqrt(f1);  f2 = sqrt(f2);
  264. }
  265.  
  266.  
  267. /* Voronoi cell noise (a.k.a. Worley noise) -- 2-D, 1-feature version. */
  268. void
  269. voronoi_f1_2d (float ss, tt;
  270.            float jitter;
  271.            output float f1;
  272.            output float spos1, tpos1;
  273.     )
  274. {
  275.     float sthiscell = floor(ss)+0.5, tthiscell = floor(tt)+0.5;
  276.     f1 = 1000;
  277.     uniform float i, j;
  278.     for (i = -1;  i <= 1;  i += 1) {
  279.     float stestcell = sthiscell + i;
  280.         for (j = -1;  j <= 1;  j += 1) {
  281.         float ttestcell = tthiscell + j;
  282.         float spos = stestcell +
  283.              jitter * (float cellnoise(stestcell, ttestcell) - 0.5);
  284.         float tpos = ttestcell +
  285.          jitter * (float cellnoise(stestcell+23, ttestcell-87) - 0.5);
  286.         float soffset = spos - ss;
  287.         float toffset = tpos - tt;
  288.         float dist = soffset*soffset + toffset*toffset;
  289.         if (dist < f1) {
  290.         f1 = dist;
  291.         spos1 = spos;  tpos1 = tpos;
  292.         }
  293.     }
  294.     }
  295.     f1 = sqrt(f1);
  296. }
  297.  
  298.  
  299. /* Voronoi cell noise (a.k.a. Worley noise) -- 2-D, 2-feature version. */
  300. void
  301. voronoi_f1f2_2d (float ss, tt;
  302.          float jitter;
  303.          output float f1;
  304.          output float spos1, tpos1;
  305.          output float f2;
  306.          output float spos2, tpos2;
  307.     )
  308. {
  309.     float sthiscell = floor(ss)+0.5, tthiscell = floor(tt)+0.5;
  310.     f1 = f2 = 1000;
  311.     uniform float i, j;
  312.     for (i = -1;  i <= 1;  i += 1) {
  313.     float stestcell = sthiscell + i;
  314.         for (j = -1;  j <= 1;  j += 1) {
  315.         float ttestcell = tthiscell + j;
  316.         float spos = stestcell +
  317.            jitter * (cellnoise(stestcell, ttestcell) - 0.5);
  318.         float tpos = ttestcell +
  319.            jitter * (cellnoise(stestcell+23, ttestcell-87) - 0.5);
  320.         float soffset = spos - ss;
  321.         float toffset = tpos - tt;
  322.         float dist = soffset*soffset + toffset*toffset;
  323.         if (dist < f1) {
  324.         f2 = f1;  spos2 = spos1;  tpos2 = tpos1;
  325.         f1 = dist;  spos1 = spos;  tpos1 = tpos;
  326.         } else if (dist < f2) {
  327.         f2 = dist;
  328.         spos2 = spos;  tpos2 = tpos;
  329.         }
  330.     }
  331.     }
  332.     f1 = sqrt(f1);  f2 = sqrt(f2);
  333. }
  334.  
  335.  
  336. #endif /* NOISES_H */
  337.